package com.zemb.flxwear.Adapter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.zemb.flxwear.R;
import com.zemb.flxwear.Utils.DeviceUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder> implements View.OnClickListener {
    private List<BleDevice> list;
    private Context context;
    private Pattern bloodPressurePattern = Pattern.compile(DeviceUtils.BLOOD_PRESSURE);
    private Pattern ecgPattern = Pattern.compile(DeviceUtils.ECG);
    private Pattern bloodOxygenPattern = Pattern.compile(DeviceUtils.BLOOD_OXYGEN);
    private Pattern bloodFourCategoryPattern = Pattern.compile(DeviceUtils.BLOOD_FOUR_CATEGORY);
    public DeviceAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    @NonNull
    @Override
    public DeviceAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_device, viewGroup, false);
        DeviceAdapter.ViewHolder viewHolder = new DeviceAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceAdapter.ViewHolder viewHolder, int position) {
        BleDevice device = list.get(position);
        if ((device.getName() + "").contains(DeviceUtils.BLOOD_PRESSURE)) {
            //血压设备
            viewHolder.iv_hp.setImageDrawable(context.getResources().getDrawable(R.mipmap.blood_pressure_green));

        } else if ((device.getName() + "").contains(DeviceUtils.ECG)) {
            //心电设备
            viewHolder.iv_hp.setImageDrawable(context.getResources().getDrawable(R.mipmap.pulse_green));

        } else if ((device.getName() + "").contains(DeviceUtils.BLOOD_OXYGEN)) {
            //血氧设备
            viewHolder.iv_hp.setImageDrawable(context.getResources().getDrawable(R.mipmap.blood_oxygen_green));
        } else if ((device.getName() + "").contains(DeviceUtils.BLOOD_FOUR_CATEGORY)) {
            //血糖设备
            viewHolder.iv_hp.setImageDrawable(context.getResources().getDrawable(R.mipmap.blood_sugar_green));
        }


        viewHolder.tv_device_name.setText(String.valueOf(device.getName() + ""));
        viewHolder.btn_connect.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setList(List<BleDevice> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addDevice(BleDevice bleDevice) {

        String deviceName = bleDevice.getName();
        if (deviceName == null) {
            return;
        }
        if (bloodPressurePattern.matcher(deviceName).find()) {
            //血压设备
            list.add(bleDevice);
        } else if (ecgPattern.matcher(deviceName).find()) {
            //心电设备
            list.add(bleDevice);
        } else if (bloodOxygenPattern.matcher(deviceName).find()) {
            //血氧设备
            list.add(bleDevice);
        } else if (bloodFourCategoryPattern.matcher(deviceName).find()) {
            //血糖设备
            list.add(bleDevice);
        }
        notifyItemChanged(list.size());

    }




    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();      //getTag()获取数据
        if (mOnItemClickListener != null) {
            switch (view.getId()) {
                case R.id.btn_connect:
                    mOnItemClickListener.onItemClick(view, list.get(position), position);
                    break;
                default:
                    //  mOnItemClickListener.onItemClick(view, DriveAdapter.ViewName.ITEM, position);
                    break;
            }
        }
    }


class ViewHolder extends RecyclerView.ViewHolder {
    //这些控件都可以设置监听事件
    private TextView tv_device_name, btn_connect;
    private ImageView iv_hp;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        tv_device_name = itemView.findViewById(R.id.tv_device_name);
        btn_connect = itemView.findViewById(R.id.btn_connect);
        iv_hp = itemView.findViewById(R.id.iv_hp);
        btn_connect.setOnClickListener(DeviceAdapter.this);

    }
}

public enum ViewName {
    ITEM,
    PRACTISE
}

//自定义一个回调接口来实现Click和LongClick事件
public interface OnItemClickListener {
    void onItemClick(View v, BleDevice device, int position);

}

    private DeviceAdapter.OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并传给外面的使用者
    public void setOnItemClickListener(DeviceAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
